var defNewLineChartOption = {
	title: {
		x: 'center'
	},
	legend: {
		// left: 'right',
		right:'25',
		top:'5'
	},
	grid: {
		left: '5%',
		right: '5%'
	},
	backgroundColor: 'white',
	tooltip: {
		trigger: 'axis',
		formatter: function(params) {
			var result = '';
			var name = '';
			for (var i = 0; i < params.length; i++) {
				var param = params[i];
				// var reverse = param.series.reverse;
				var reverse = param.reverse;
				if (i == 0) {
					name = param.name;
					result += name;
				}
				result += '<br/>' + param.seriesName + ':' + (param.data * (reverse ? -1 : 1));
			}
			return result;
		}
	},
	toolbox: {
		feature: {
            dataView: {
            	show: true,
            	title : '保存为图片',
            	click : function (){
                    alert('myToolHandler')
                }
            }
        },
		show: true
	},
	xAxis: [{
		type: 'category',
		axisLine: {
			onZero: false
		}
	}],
	yAxis: [],
	series: []
};

function newLineExtend(data) {
	//标记是否含有柱状图
	var hasBar = false;
	var chartOption = $.extend(true, {}, defNewLineChartOption);
	var serie = {
		type: 'line',
		itemStyle: {
			normal: {
				borderWidth: '1px'
			}
		},
		smooth: true,
		symbol: 'none'
	};

	function buildYAxisParams(axis, model) {
		var formatFun,
			min = axis.min,
			max = axis.max,
			reverse = axis.reverse;

		// if (reverse) { // 反转
		// 	var tmpMax = -max,
		// 		tmpMin = -min;
		// 	min = tmpMax;
		// 	max = tmpMin;
		// }
		if (model == 'left') {
			formatFun = leftLabelFormatter;
		} else if (model == 'right') {
			formatFun = rightLabelFormatter;
		}

		var json = {
			type: 'value',
			name: axis.name,
			axisLabel: {
				formatter: formatFun
			}
		}
		if (reverse) {
			json = $.extend(json, {
				inverse: reverse,
				nameLocation: 'start'
			});
		}
		if (!hasBar) {
			json = $.extend(json, {
				min: min,
				max: max
			});
		}

		return json;
	}

	function leftLabelFormatter(value) {
		return yAxisLabelFormatter(value, data.leftYAxis.reverse)
	}

	function rightLabelFormatter(value) {
		return yAxisLabelFormatter(value, data.rightYAxis.reverse)
	}

	function yAxisLabelFormatter(value, reverse) {
		if ($.isNumeric(value)) { //判断是不是数字
			var valueStr = value.toString();
			var dokIndex = valueStr.indexOf('.');
			if (dokIndex != -1) {
				var valLen = valueStr.length;
				var douNum = (valLen - (dokIndex + 1));
				if (douNum >= 3) {
					value = parseFloat(value.toFixed(2));
				}
			}
			// if (reverse) { //反向
			// 	return value * -1;
			// }
		}
		return value;
	}



	var legendData = [];
	for (var i = 0; i < data.chartLineParams.length; i++) {
		var chartLineParam = data.chartLineParams[i];
		var newSerie = $.extend(true, {}, serie);
		var axisType = chartLineParam.axisType;
		if (axisType) { //定义chart类型
			newSerie.type = axisType;

			if (axisType == 'bar' && !hasBar) {
				hasBar = true;
			}
		}

		var reverse = false
		if (chartLineParam.axisOption == 'leftYAxis') {
			newSerie.yAxisIndex = 0;
			reverse = data.leftYAxis.reverse;
		} else if (chartLineParam.axisOption == 'rightYAxis') {
			newSerie.yAxisIndex = 1;
			reverse = data.rightYAxis.reverse;
		}
		newSerie.name = chartLineParam.axisName;
		if (reverse) { //特殊处理数据
			var colDatas = chartLineParam.colDatas;
			// newSerie.data = (function() {
			// 	var oriData = colDatas;
			// 	var len = oriData.length;
			// 	while (len--) {
			// 		oriData[len] *= -1;
			// 	}
			// 	return oriData;
			// })();
			newSerie.data = colDatas;

		} else {
			newSerie.data = chartLineParam.colDatas;
		}
		newSerie.reverse = reverse;
		chartOption.series.push(newSerie);
		legendData.push(chartLineParam.axisName);
	}

	chartOption.title.text = data.chartTitle;
	chartOption.legend.data = legendData;

	chartOption.xAxis[0].name = data.xAxis.colName;
	chartOption.xAxis[0].data = data.xAxis.colDatas;
	// 两端是否留白 true 留白  false 不留白
	chartOption.xAxis[0].boundaryGap = hasBar;


	if (!$.isEmptyObject(data.leftYAxis)) { //左
		var axis = data.leftYAxis;
		chartOption.yAxis.push(buildYAxisParams(axis, 'left'));
	}

	if (!$.isEmptyObject(data.rightYAxis)) { //右
		var axis = data.rightYAxis;
		chartOption.yAxis.push(buildYAxisParams(axis, 'right'));
	}


	return chartOption;
}